package view;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

import controller.ContactControllerListener;
import po.Room;
import po.Student;
import service.ContactService;


public class ContactFrame extends JFrame{
	private JPanel tablePanel;
	JToolBar toolBar;
	private ContactService contactService;
	private long studentNo;
	private JSplitPane split;
	private Box box;
	private ContactControllerListener controller;
	private JTable table;
	private JTextField text2;
	public ContactFrame()  {
		controller = new ContactControllerListener();
		contactService = new ContactService();
		controller.setContactFrame(this);
		controller.setContactService(contactService);
		
		contactService = new ContactService();
		setLayout(new FlowLayout());
        box = getBox();
		setContentPane(box);
		this.setBounds(200, 200, 1200, 800);
		this.setVisible(true);
		
	}
	
	public Component getBookModofyToolBar() {
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		toolBar.setBorder(new BevelBorder(BevelBorder.RAISED));
		toolBar.setAlignmentX(RIGHT_ALIGNMENT);
		JButton addStudentButton = new JButton("添加");
		ImageIcon addIcon = new ImageIcon(ContactFrame.class.getResource("/res/User Plus.png"));
		addStudentButton.setIcon(addIcon);
		addStudentButton.setActionCommand("addStudentDialog");
		addStudentButton.addActionListener(controller);
		JButton searchStudentButton = new JButton("搜索");
		ImageIcon searchIcon = new ImageIcon(ContactFrame.class.getResource("/res/search.png"));
		searchStudentButton.setIcon(searchIcon);
		searchStudentButton.setActionCommand("searchStudentDialog");
		searchStudentButton.addActionListener(controller);
		toolBar.add(addStudentButton);
		toolBar.add(searchStudentButton);
		return toolBar;
		
	}
	public JPanel getStudentTablePanel(List<Student> studentList) {
		tablePanel = new JPanel();
		String titles[] = {"姓名","学号"};
		Object data[][] = new Object[studentList.size()][2];
		Iterator<Student> iter = studentList.iterator();
		int i =0;
		while(iter.hasNext()) {
			Student student = iter.next();
			data[i][0] = student.getStudentName();
			data[i][1] = student.getStudentNo();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data,titles);
		tableModel.setRowCount(100);
		table = new JTable(tableModel);
		table.getTableHeader().setReorderingAllowed(false);
		table.setRowHeight(40);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane jScrollPane = new JScrollPane(table);
		jScrollPane.setPreferredSize(new Dimension(500, 700));
		tablePanel.add(jScrollPane);
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				int selRow = table.getSelectedRow();  //得到选中的行
				studentNo = Long.parseLong(table.getValueAt(selRow, 1).toString());
				System.out.println("studentNo:"+studentNo);
				split.setRightComponent(getPanel(studentNo));
			}
		});
		return tablePanel;
		
	}
	public Box getBox() {
		Box hBox1 = Box.createHorizontalBox();
		Box hBox2 = Box.createHorizontalBox();	
		Component rigidArea1 = Box.createRigidArea(new Dimension(1100, 50));
		hBox1.add(getBookModofyToolBar());
		hBox1.add(rigidArea1);
		split = new JSplitPane();
		split.setLeftComponent(getStudentTablePanel(contactService.findAll()));
		split.setRightComponent(Box.createRigidArea(new Dimension(500, 700)));
		split.setEnabled(false);
		split.setDividerSize(0);
		hBox2.add(split);
		Box vBox = Box.createVerticalBox();
        vBox.add(hBox1);
        vBox.add(hBox2);
        return vBox;
	}
	public JPanel getPanel(long studentNo) {
		JPanel panel = new JPanel();
		Student student = new Student();
		student=contactService.getStudent(studentNo);
		Room room = new Room();
		Box vBox = Box.createVerticalBox();
		Box hBox = Box.createHorizontalBox();
		room =contactService.get(student.getRoom().getRoomNo());
		JLabel label1 = new JLabel("姓名");
		JTextField text1 = new JTextField(student.getStudentName());
		JLabel label2 = new JLabel("学号");
		text2 = new JTextField(student.getStudentNo().toString());
		JLabel label3 = new JLabel("生日");
		JTextField text3 = new JTextField(student.getBirthday().toString());
		JLabel label4 = new JLabel("电话");
		JTextField text4 = new JTextField(student.getTel());
		JLabel label5 = new JLabel("邮箱");
		JTextField text5 = new JTextField(student.getEmail());
		JLabel label6 = new JLabel("房间");
		JTextField text6 = new JTextField(room.getRoomName());
		JLabel label7 = new JLabel("籍贯");
		JTextField text7 = new JTextField(student.getNPlace());
		JLabel label8 = new JLabel("家庭地址");
		JTextField text8 = new JTextField(student.getAddress());
		vBox.add(label1);
		vBox.add(text1);
		vBox.add(label2);
		vBox.add(text2);
		vBox.add(label3);
		vBox.add(text3);
		vBox.add(label4);
		vBox.add(text4);
		vBox.add(label5);
		vBox.add(text5);
		vBox.add(label6);
		vBox.add(text6);
		vBox.add(label7);
		vBox.add(text7);
		vBox.add(label8);
		vBox.add(text8);
		JButton deleteStudentButton = new JButton("删除");
		ImageIcon deleteIcon = new ImageIcon(ContactFrame.class.getResource("/res/delete.png"));
		deleteStudentButton.setIcon(deleteIcon);
		deleteStudentButton.setActionCommand("deleteStudentDialog");
		deleteStudentButton.addActionListener(controller);
		JButton updateStudentButton = new JButton("修改");
		ImageIcon updateIcon = new ImageIcon(ContactFrame.class.getResource("/res/update.png"));
		updateStudentButton.setIcon(updateIcon);
		updateStudentButton.setActionCommand("updateStudentDialog");
		updateStudentButton.addActionListener(controller);
		hBox.add(deleteStudentButton);
		hBox.add(Box.createRigidArea(new Dimension(10, 5)));
		hBox.add(updateStudentButton);
		vBox.add(Box.createRigidArea(new Dimension(5, 25)));
		vBox.add(hBox);
		panel.add(vBox);
		panel.setBounds(600, 50, 600, 700);
		return panel;
	}
	public void showSearchResult(Student result) {
		this.remove(box);
		setContentPane(box);
		split.setRightComponent(getPanel(result.getStudentNo()));
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");
	}
	public void refresh() {
		this.remove(box);
		split.setLeftComponent(getStudentTablePanel(contactService.findAll()));
		split.setRightComponent(Box.createRigidArea(new Dimension(500, 700)));
		setContentPane(box);
		this.validate();
	}

	public ContactControllerListener getController() {
		return controller;
	}

	public void setController(ContactControllerListener controller) {
		this.controller = controller;
	}

	public JTable getTable() {
		return table;
	}

	public JSplitPane getSplit() {
		return split;
	}

	public JTextField getText2() {
		return text2;
	}
	
}
